#!/usr/bin/env python
# encoding: utf-8

WATER_CMD = {
    '开高压': ['AA', '0000', '00', '01', '', '00'],
    '关高压': ['AA', '0000', '00', '02', '', '00'],
    '本底测量': ['AA', '0000', '00', '72', '', '00'],
    '开始测量': ['AA', '0000', '00', '71', '', '00'],
    '停止测量': ['AA', '0000', '00', '73', '', '00'],
    '单次测量': ['AA', '0000', '00', '74', '', '00'],
    '设置传输速度': ['AA', '0000', '00', '41', '', '00'],
    '设置走位速度': ['AA', '0000', '00', '07', '', '00'],
    '回程差x': ['AA', '0000', '00', '21', '', '00'],
    '回程差y': ['AA', '0000', '00', '22', '', '00'],
    '回程差z': ['AA', '0000', '00', '23', '', '00'],
    '回程差': ['AA', '0000', '00', '52', '', '00'],
    '电机走步x+': ['AA', '0000', '00', '32', '', '00'],
    '电机走步x-': ['AA', '0000', '00', '31', '', '00'],
    '电机走步y+': ['AA', '0000', '00', '34', '', '00'],
    '电机走步y-': ['AA', '0000', '00', '33', '', '00'],
    '电机走步z+': ['AA', '0000', '00', '36', '', '00'],
    '电机走步z-': ['AA', '0000', '00', '35', '', '00'],
    '电机走步': ['AA', '0000', '00', '51', '', '00'],
    '连续测量': ['AA', '0000', '00', '50', '', '00'],
    '数据写入': ['AA', '0000', '00', '42', '', '00'],
    '数据读取': ['AA', '0000', '00', '43', '', '00'],
    '复位坐标': ['AA', '0000', '00', '44', '', '00'],
    '读取坐标': ['AA', '0000', '00', '53', '', '00'],
    # '读取限位': ['AA', '0000', '00', '6A', '', '00'],
    '读取限位': ['AA', '0000', '00', '6B', '', '00'],
    '读取信息': ['AA', '0000', '00', '82', '', '00'],
    '设置限位': ['AA', '0000', '00', '6A', '', '00'],
    '移动停止': ['AA', '0000', '00', '54', '', '00'],
    '停止':     ['AA', '0000', '00', '55', '', '00'],
    '任务坐标': ['AA', '0000', '00', '5A', '', '00'],
    'x+': ['AA', '0000', '00', '45', '', '00'],
    'x-': ['AA', '0000', '00', '46', '', '00'],
    'y+': ['AA', '0000', '00', '47', '', '00'],
    'y-': ['AA', '0000', '00', '48', '', '00'],
    'z+': ['AA', '0000', '00', '49', '', '00'],
    'z-': ['AA', '0000', '00', '4A', '', '00'],
    'lowspeed': ['AA', '0000', '00', '4B', '', '00'],
    'highspeed': ['AA', '0000', '00', '4C', '', '00'],
}

class Help:

    pdd_data = [
    [0,35544,11115],
    [2,34818,10927],
    [4,35072,11035],
    [6,37858,11268],
    [8,39486,11201],
    [10,40309,11196],
    [11,40743,11180],
    [12,41804,11407],
    [13,42275,11493],
    [14,41970,11441],
    [15,40676,11138],
    [16,41868,11623],
    [17,41496,11634],
    [18,40790,11689],
    [19,39466,11717],
    [20,36998,11497],
    [21,33676,11029],
    [22,31754,11081],
    [23,29583,11223],
    [24,26927,11247],
    [25,23654,11230],
    [26,20398,11238],
    [27,16870,11002],
    [28,13923,11122],
    [29,11150,11227],
    [30,8702,11145],
    [50,343,11206],
    [70,302,10983],
    [90,272,11280],
    [110,247,11287],
    [130,220,10875],
    [150,204,11018],
    [170,187,11145],
    [180,178,11015],
    ]

    def __init__(self):
        pass

    def HexToNeg(self, hex_str):
        """十六进制字符串生成数字，支持负数"""
        if hex_str[0] == 'F' or hex_str[0] == 'f':
            return int(hex_str, 16) - (1 << 32)
        else:
            return int(hex_str, 16)

    def cmd_build(self, cmd_list):
        '''生成命令'''
        cmd = ''.join(cmd_list)
        cmd.upper()
        cmd_start = cmd_list[0]
        cmd_len = hex(int(len(cmd) / 2)).replace('0x', '').zfill(4).upper()
        cmd_list[1] = cmd_len
        if len(cmd_len) == 4:
            hex_len = int(cmd_len[:2], 16) + int(cmd_len[-2:], 16)
            cmd_sum = (hex(int(cmd_start, 16) + int(cmd_len[:2], 16) + int(cmd_len[-2:], 16))).upper()[-2:]
        if len(cmd_len) == 2:
            hex_len = int(cmd_len, 16)
            cmd_sum = (hex(int(cmd_start, 16) + int(cmd_len, 16))).upper()[-2:]
        cmd_list[2] = cmd_sum
        s = 0
        s = int(cmd_list[0], 16) + hex_len + int(cmd_list[2], 16) + int(cmd_list[3], 16)
        for i in range(0, len(cmd_list[4])):
            if i % 2 == 0:
                s = s + int(cmd_list[4][i:i + 2], 16)
        s = hex(s).upper()[-2:]
        cmd_list[-1] = s
        cmd = ''.join(cmd_list)
        return cmd

